<?php
namespace app\models;
use yii\base\Model;

class ContentSearch extends Content
{
    public $hassamelevel;//包含同级的内容，比如XX内容在A栏目，也在B栏目，在取B栏目时，也要取出XX内容
    public $catids_by_tag;//根据TAG查出的catid(多个)
    public $begin_time;
    public $end_time;
    public $sort_flag;//排序方式 0-默认 1-预览总数正序 2-预览总数倒序
    public $haschild;//是否包含子类内容 1-不包含2-包含

    public function rules()
    {
        return [
            ['hassamelevel', 'in', 'range' => array(0, 1), 'message' => 'state值必须在0~1中选择'],
            ['catids_by_tag', 'string', 'message' => 'catids_by_tag必须是字符串'],
            ['catid', 'required', 'message' => 'catid不能为空', 'on' => 'add'],
            ['catid', 'integer', 'message' => 'catid必须为数字，或数字字符串', 
                     'min'=>0, 'tooSmall' => 'catid最小存储0', 
                     'max'=>65535, 'tooBig' => 'catid最大存储65535'],
            // ['title', 'default', 'value' => ''],
            ['title', 'string', 'message' => 'title必须是字符串',
                             'max' => 32, 'tooLong' => 'title最长32位'],
            // ['state', 'default', 'value' => 0],
            ['state', 'integer', 'message' => 'state必须为数字，或数字字符串'],
            ['state', 'in', 'range' => array(0, 1, 2, 3, 4, 5), 'message' => 'state值必须在0~5中选择'],
            ['member_view', 'default', 'value' => 0],
            ['member_view', 'integer', 'message' => 'member_view必须为数字，或数字字符串'],
            ['member_view', 'in', 'range' => array(0, 1, 2), 'message' => 'member_view值必须在0~2中选择'],
            ['begin_time', 'date', 'format'=>'yyyy-MM-dd', 'message' => 'begin_time非yyyy-MM-dd格式'],
            ['end_time', 'date', 'format'=>'yyyy-MM-dd', 'message' => 'end_time非yyyy-MM-dd格式'],
            ['sort_flag', 'default', 'value' => 0],
            ['sort_flag', 'integer', 'message' => 'sort_flag必须为数字，或数字字符串'],
            ['sort_flag', 'in', 'range' => array(0, 1, 2), 'message' => 'sort_flag值必须在0~2中选择'],
            ['haschild', 'integer', 'message' => 'haschild必须为数字，或数字字符串'],
            ['haschild', 'in', 'range' => array(1, 2), 'message' => 'haschild值必须在1~2中选择'],
            ['display', 'integer', 'message' => 'display必须为数字，或数字字符串'],
            ['display', 'in', 'range' => array(1, 2), 'message' => 'display值必须在1,2中选择'],

        ];
    }

    public function scenarios()
    {
        return Model::scenarios();
    }

    public function search($params)
    {
        $data['ContentSearch'] = $params;
        // P($data);

        $query = Content::find();

        if (!($this->load($data) && $this->validate())) {
            // if($this->hasErrors()){
            //     P($this->getErrors());
            // }
            return false;
        }
        // P($this);
        if(isset($this->catids_by_tag)){
            $query -> orFilterWhere(
                ['in', '{{%content}}.content_id', explode(',', $this->catids_by_tag)]
            );
        }

        if(isset($this->catid) and !isset($this->haschild) and !isset($this->hassamelevel)){
            $query -> andFilterWhere([
                '{{%content}}.catid' => $this->catid,
            ]);
        }


        if(isset($this->state)){
            $query -> andFilterWhere([
                'state' => $this->state,
            ]);
        }

        if(isset($this->display)){
            $query -> andFilterWhere([
                'display' => $this->display,
            ]);
        }

        if(isset($this->title)){
            $query -> andFilterWhere(
                ['like', '{{%content}}.title', $this->title]
            );
        }


        if($this->member_view > 0){
            $query -> andFilterWhere([
                'member_view' => $this->member_view,
            ]);
        }


        if(isset($this->end_time) and !empty($this->end_time)){
            $this->end_time .= ' 23:59:59';
        }
        $query -> andFilterWhere(['>=', '{{%content}}.add_time', $this->begin_time])
               -> andFilterWhere(['<=', '{{%content}}.add_time', $this->end_time]);

        // P($query);
        return $query;
    }

    
}